package com.burger.controller;

import com.burger.dao.LoginDao;
import com.burger.domain.Book;
import com.burger.domain.User;
import com.burger.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/users")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/login")
    public Result login(@RequestBody User user) {
        boolean flag = loginService.login(user);
       // System.out.println(flag);
        String msg = flag != false ? "登录成功" : "登录失败！";
        Integer code = flag != false ? Code.LOGIN_SUCCESS : Code.LOGIN_ERR;
        return new Result(code,user,msg);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        boolean flag = loginService.register(user);
        System.out.println(flag);
        String msg = flag != false ? "注册成功" : "注册失败！";
        Integer code = flag != false ? Code.REGISTER_SUCCESS : Code.REGISTER_ERR;
        return new Result(code,user,msg);
    }


    @GetMapping
    public Result findAll(){
        List<User> userList = loginService.findAll();
        Integer code = userList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = userList != null ? "" : "数据查询失败，请重试！";
        return new Result(code,userList,msg);
    }

}
